package com.ruoyi.inducePest.service.device.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.inducePest.domain.device.DeviceLureChangeRecord;
import com.ruoyi.inducePest.mapper.device.InduceDeviceLureChangeRecordMapper;
import com.ruoyi.inducePest.service.device.InduceDeviceLureChangeRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @program: ruoyi_202130504106_李航
 * @description:
 * @author: LiHang
 * @create: 2022-04-11 10:12
 **/

@Slf4j
@Service
public class InduceDeviceLureChangeRecordServiceImpl implements InduceDeviceLureChangeRecordService {



    @Autowired
    private InduceDeviceLureChangeRecordMapper induceDeviceLureChangeRecordMapper;



    /**
     * 存储文件的路径
     */
    @Value("${ruoyi.profile}")
    private String profile;


    @Override
    public TableDataInfo selectInfoByCondition(DeviceLureChangeRecord deviceLureChangeRecord, Integer pageNum, Integer pageSize) {
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<DeviceLureChangeRecord> infoByCondition = induceDeviceLureChangeRecordMapper.getInfoByCondition(deviceLureChangeRecord);
        PageInfo<DeviceLureChangeRecord> pageInfo = new PageInfo<>(infoByCondition);
        return getTableDataInfo(infoByCondition,pageInfo);
    }

    @Override
    public int insertChangeRecord(DeviceLureChangeRecord deviceLureChangeRecord) {
        deviceLureChangeRecord.setUpdateTime(new Date());

        Long userIdL = SecurityUtils.getLoginUser().getUser().getUserId();
        Integer userId=Integer.valueOf(userIdL.intValue());
        deviceLureChangeRecord.setUserId(userId);
        return induceDeviceLureChangeRecordMapper.insertChangeInfo(deviceLureChangeRecord);
    }

    private TableDataInfo getTableDataInfo(List<DeviceLureChangeRecord> changeRecords, PageInfo<DeviceLureChangeRecord> pageInfo) {

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(changeRecords);
        rspData.setTotal(pageInfo.getTotal());
        return rspData;
    }
}
